package edu.gatech.cs2340.group7.wheresmystuffapp.database;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Account;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Admin;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.CustomDate;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.DonatedItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.FoundItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.Item;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.ItemLocation;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.LostItem;
//import edu.gatech.cs2340.group7.wheresmystuffapp.model.PictureManager;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.RequestedDonationItem;
import edu.gatech.cs2340.group7.wheresmystuffapp.model.User;

/**
 * 
 * @author tbowling3
 * 
 */
public class WMSSQLiteDatabase implements IDatabase {
	private MyDbHelper myHelper;
	// private final Context myContext;
	private SQLiteDatabase myDatabase;
	private String[] allUserColumns = { MyDbHelper.KEY_NAME,
			MyDbHelper.KEY_USER_PASSWORD, MyDbHelper.KEY_ACCOUNT_CREATED_DATE,
			MyDbHelper.KEY_ACCOUNT_LOCKED, MyDbHelper.KEY_ACCOUNT_PHONE,
			MyDbHelper.KEY_IS_ADMIN };

	/*
	 * private String[] allItemColumns = {MyDbHelper.KEY_ROWID,
	 * MyDbHelper.KEY_ITEM_NAME, MyDbHelper.KEY_ITEM_USER_NAME,
	 * MyDbHelper.KEY_CATEGORY, MyDbHelper.KEY_ITEM_DESCRIPTION,
	 * MyDbHelper.KEY_ITEM_RESOLVE, MyDbHelper.KEY_LOCATION,
	 * MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED};
	 */

	public WMSSQLiteDatabase(Context context) {
		myHelper = new MyDbHelper(context);
	}

	public void open() throws SQLException {
		myDatabase = myHelper.getWritableDatabase();
	}

	public void close() {
		myHelper.close();
	}

	/**
	 * Converts a cursor object to an account object
	 * 
	 * @param c
	 *            Cursor that was made from a query of the account table
	 * @return The user object created from the cursor
	 */
	@SuppressWarnings("deprecation")
	private Account cursorToAccount(Cursor c) {
		Account out = null;
		if (c != null) {
			c.moveToFirst();
			System.out.println("Getting username");
			String user = c.getString(c.getColumnIndex(MyDbHelper.KEY_NAME));
			System.out.println("Got username, getting password");
			String password = c.getString(c
					.getColumnIndex(MyDbHelper.KEY_USER_PASSWORD));
			System.out.println("Getting date");
			String dateCreated = c.getString(c
					.getColumnIndex(MyDbHelper.KEY_ACCOUNT_CREATED_DATE));
			System.out.println("Getting isLocked");
			boolean isLocked = intToBool(c.getInt(c
					.getColumnIndex(MyDbHelper.KEY_ACCOUNT_LOCKED)));
			System.out.println("Getting phone #");
			String phone = c.getString(c
					.getColumnIndex(MyDbHelper.KEY_ACCOUNT_PHONE));
			System.out.println("Getting isAdmin");
			boolean isAdmin = intToBool(c.getInt(c
					.getColumnIndex(MyDbHelper.KEY_IS_ADMIN)));

			System.out.println("All info gotten, creating account");
			if (isAdmin) {
				out = new Admin(user, password, phone);
			} else {
				out = new User(user, password, phone);
			}
			System.out.println("Account Created");
		//	out.setAccountDate(new Date(dateCreated));
			out.setLocked(isLocked);
		}
		System.out.println("Returning account, username is: " + out.getEmail());
		return out;
	}

	/**
	 * Converts a cursor object to a found item object
	 * 
	 * @param c
	 *            Cursor that was made from a query of the item tables
	 * @return The found item object created from the cursor
	 */
	private FoundItem cursorToFoundItem(Cursor cursor) {
		String itemName = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_NAME));
		String description = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DESCRIPTION));
		String category = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_CATEGORY));
		boolean isResolved = intToBool(cursor.getInt(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_RESOLVE)));
		int lat = cursor.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LATITUDE));
		int lon = cursor
				.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LONGITUDE));
		byte[] pictureId = cursor.getBlob(cursor
				.getColumnIndex(MyDbHelper.KEY_PICTURE_ID));
		String username = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_USER_NAME));

		String dateEntered = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DATE_ENTERED));
		CustomDate dateE = new CustomDate(dateEntered);

		String dateFound = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_DATE_FOUND));
		CustomDate dateF = new CustomDate(dateFound);

		ItemLocation il = new ItemLocation(lat, lon);

		FoundItem item = new FoundItem(username, itemName, category,
				description, il, dateF, pictureId);
		item.setDateEntered(dateE);
		item.setResolved(isResolved);
		return item;
	}

	/**
	 * Converts a cursor object to a lost item object
	 * 
	 * @param c
	 *            Cursor that was made from a query of the item tables
	 * @return The lost item object created from the cursor
	 */
	private LostItem cursorToLostItem(Cursor cursor) {
		String itemName = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_NAME));
		String description = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DESCRIPTION));
		String category = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_CATEGORY));
		boolean isResolved = intToBool(cursor.getInt(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_RESOLVE)));
		int lat = cursor.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LATITUDE));
		int lon = cursor
				.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LONGITUDE));
		byte[] pictureId = cursor.getBlob(cursor
				.getColumnIndex(MyDbHelper.KEY_PICTURE_ID));
		String username = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_USER_NAME));

		String dateEntered = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DATE_ENTERED));
		CustomDate dateE = new CustomDate(dateEntered);

		String dateLost = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_DATE_LOST));
		CustomDate dateL = new CustomDate(dateLost);
		int reward = cursor.getInt(cursor
				.getColumnIndex(MyDbHelper.KEY_LOST_ITEM_REWARD));

		ItemLocation itemLoc = new ItemLocation(lat, lon);

		LostItem item = new LostItem(username, category, itemName, description,
				itemLoc, reward, dateL, pictureId);
		item.setDateEntered(dateE);
		item.setResolved(isResolved);
		return item;
	}

	@SuppressWarnings("deprecation")
	@Override
	public long addAccount(Account account) {
		String name = account.getEmail();
		String password = account.getPassword();
		String phone = account.getPhone();
		Date date = account.getAccountDate();
		boolean isLocked = account.isLocked();
		boolean isAdmin = account.isAdmin();

		ContentValues cv = new ContentValues();
		cv.put(MyDbHelper.KEY_NAME, name);
		cv.put(MyDbHelper.KEY_USER_PASSWORD, password);
		cv.put(MyDbHelper.KEY_ACCOUNT_CREATED_DATE, date.getMonth() + "-"
				+ date.getDay() + "-" + date.getYear());
		cv.put(MyDbHelper.KEY_ACCOUNT_LOCKED, boolToInt(isLocked));
		cv.put(MyDbHelper.KEY_ACCOUNT_PHONE, phone);
		cv.put(MyDbHelper.KEY_IS_ADMIN, boolToInt(isAdmin));
		System.out
				.println("cv was created and attributes were added. The phone number is "
						+ phone);
		long out = myDatabase.insert(MyDbHelper.USER_TABLE_NAME, null, cv);
		System.out.println("The account was added to the database at row "
				+ out);
		return out;
	}

	@Override
	public long addItem(Item item) {
		long out = -1;
		if (item.isLostItem()) {
			out = addLostItem((LostItem) item);
		} else {
			out = addFoundItem((FoundItem) item);
		}
		return out;
	}

	/**
	 * Adds a lost item to the database
	 * 
	 * @param item
	 *            Item to be added
	 * @return The row of the item in the Item Table
	 */
	private long addLostItem(LostItem item) {
		long out = -1;

		String itemName = item.getName();
		String user = item.getUsername();
		String category = item.getCategory();
		String description = item.getDescription();
		ItemLocation location = item.getItemLocation();
		CustomDate dateEntered = item.getDateEntered();
		String dateE = dateEntered.getDateEntered();
		CustomDate dateLost = item.getDateLost();
		String dateL = dateLost.getDateEntered();
		int reward = item.getReward();
		int isResolved = boolToInt(item.isResolved());
		byte[] pictureId = item.getPicture(); // TODO

		ContentValues cv = new ContentValues();

		cv.put(MyDbHelper.KEY_ITEM_USER_NAME, user);
		cv.put(MyDbHelper.KEY_CATEGORY, category);
		cv.put(MyDbHelper.KEY_ITEM_NAME, itemName);
		cv.put(MyDbHelper.KEY_ITEM_DESCRIPTION, description);
		cv.put(MyDbHelper.KEY_LATITUDE, location.getLatitude());
		cv.put(MyDbHelper.KEY_LONGITUDE, location.getLongitude());
		cv.put(MyDbHelper.KEY_ITEM_DATE_ENTERED, dateE);
		cv.put(MyDbHelper.KEY_ITEM_RESOLVE, isResolved);
		cv.put(MyDbHelper.KEY_PICTURE_ID, pictureId);

		out = myDatabase.insert(MyDbHelper.ITEM_TABLE_NAME, null, cv);

		ContentValues cv2 = new ContentValues();
		cv2.put(MyDbHelper.LOST_ITEM_ID, out);
		cv2.put(MyDbHelper.KEY_LOST_ITEM_NAME, itemName);
		cv2.put(MyDbHelper.KEY_LOST_ITEM_REWARD, reward);
		cv2.put(MyDbHelper.KEY_DATE_LOST, dateL);
		myDatabase.insert(MyDbHelper.LOST_ITEM_TABLE_NAME, null, cv2);
		System.out.println("Lost item: " + itemName + " added");
		return out;
	}

	/**
	 * Adds a found item to the database
	 * 
	 * @param item
	 *            Item to be added
	 * @return The row of the item in the Item Table
	 */
	private long addFoundItem(FoundItem item) {
		long out = -1;
		String itemName = item.getName();
		String user = item.getUsername();
		String category = item.getCategory();
		String description = item.getDescription();
		ItemLocation location = item.getItemLocation();
		CustomDate dateEntered = item.getDateEntered();
		String dateE = dateEntered.getDateEntered();
		int isResolved = boolToInt(item.isResolved());
		byte[] pictureId = item.getPicture(); // TODO

		CustomDate dateFound = item.getDateFound();
		String dateF = dateFound.getDateEntered();

		ContentValues cv = new ContentValues();

		cv.put(MyDbHelper.KEY_ITEM_USER_NAME, user);
		cv.put(MyDbHelper.KEY_CATEGORY, category);
		cv.put(MyDbHelper.KEY_ITEM_NAME, itemName);
		cv.put(MyDbHelper.KEY_ITEM_DESCRIPTION, description);
		cv.put(MyDbHelper.KEY_LATITUDE, location.getLatitude());
		cv.put(MyDbHelper.KEY_LONGITUDE, location.getLongitude());
		cv.put(MyDbHelper.KEY_ITEM_DATE_ENTERED, dateE);
		cv.put(MyDbHelper.KEY_ITEM_RESOLVE, isResolved);
		cv.put(MyDbHelper.KEY_PICTURE_ID, pictureId);

		out = myDatabase.insert(MyDbHelper.ITEM_TABLE_NAME, null, cv);

		ContentValues cv2 = new ContentValues();
		cv2.put(MyDbHelper.KEY_FOUND_ITEM_NAME, itemName);
		cv2.put(MyDbHelper.FOUND_ITEM_ID, out);
		cv2.put(MyDbHelper.KEY_DATE_FOUND, dateF);
		myDatabase.insert(MyDbHelper.FOUND_ITEM_TABLE_NAME, null, cv2);

		System.out.println("Found item: " + itemName + " added");
		return out;
	}

	@Override
	public List<Item> getItems(String username) {
		List<Item> out = getLostItems(username);
		out.addAll(getFoundItems(username));
		return out;
	}

	@Override
	public List<Item> getLostItems(String username) {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED,
				MyDbHelper.KEY_DATE_LOST, MyDbHelper.KEY_LOST_ITEM_REWARD };
		String whereClause = MyDbHelper.KEY_ROWID + "=" + MyDbHelper.LOST_ITEM_ID + " AND "
				+ MyDbHelper.KEY_ITEM_USER_NAME + "=? ";

		Cursor c = myDatabase.query(MyDbHelper.LOST_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, new String[] {username}, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToLostItem(c));
		}
		return out;
	}

	@Override
	public List<Item> getFoundItems(String username) {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED,
				MyDbHelper.KEY_DATE_FOUND };
		String whereClause = MyDbHelper.KEY_ROWID + "=" + MyDbHelper.FOUND_ITEM_ID + " AND "
				+ MyDbHelper.KEY_ITEM_USER_NAME
				+ "=?";

		Cursor c = myDatabase.query(MyDbHelper.FOUND_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, new String[] { username}, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToFoundItem(c));
		}
		return out;
	}

	@Override
	public List<Item> getAllLostItems() {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED,
				MyDbHelper.KEY_DATE_LOST, MyDbHelper.KEY_LOST_ITEM_REWARD };
		//String whereClause = MyDbHelper.KEY_ROWID + "=?";
		//String[] whereArgs = new String[] {MyDbHelper.LOST_ITEM_ID};
		
		String whereClause= MyDbHelper.KEY_ROWID + "=" + MyDbHelper.LOST_ITEM_ID;
		String[] whereArgs = null;
		
		Cursor c = myDatabase.query(MyDbHelper.LOST_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, whereArgs, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToLostItem(c));
		}
		return out;
	}

	@Override
	public List<Item> getAllFoundItems() {
		// TODO Auto-generated method stub
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED,
				MyDbHelper.KEY_DATE_FOUND };
		//String whereClause = MyDbHelper.KEY_ROWID + "=?";
		//String[] whereArgs = new String[] {MyDbHelper.FOUND_ITEM_ID};
		
		String whereClause = MyDbHelper.KEY_ROWID + "=" + MyDbHelper.FOUND_ITEM_ID;
		String[] whereArgs = null; 
		
		Cursor c = myDatabase.query(MyDbHelper.FOUND_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, whereArgs, null, null, null);
		
		System.out.println("The cursor found " + c.getCount() + " items");
		
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToFoundItem(c));
		}
		return out;
	}

	@Override
	public Account deleteAccount(String username) {
		Account out = getAccount(username);
		myDatabase.delete(MyDbHelper.USER_TABLE_NAME, MyDbHelper.KEY_NAME
				+ "=?", new String[] { username });
		return out;
	}

	@Override
	public Account getAccount(String username) {
		System.out.println("the username is: " + username);
		Cursor c = myDatabase.query(MyDbHelper.USER_TABLE_NAME, allUserColumns,
				MyDbHelper.KEY_NAME + "=?", new String[] { username }, null,
				null, null);
		System.out.println(c.toString());
		return cursorToAccount(c);
	}

	@Override
	public List<Item> getAllItems() {
		List<Item> out = getAllLostItems();
		out.addAll(getAllFoundItems());
		return out;
	}

	@Override
	public List<Account> getAllAccounts() {
		List<Account> out = new ArrayList<Account>();
		Cursor c = myDatabase.query(MyDbHelper.USER_TABLE_NAME, allUserColumns,
				null, null, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToAccount(c));
		}
		return out;
	}

	@Override
	public boolean accountExists(String username) throws SQLException {
		boolean out = false;

		Cursor c = myDatabase.query(MyDbHelper.USER_TABLE_NAME, allUserColumns,
				MyDbHelper.KEY_NAME + "= ?", new String[] { username }, null,
				null, null, null);
		if (c.getCount() == 1) {
			out = true;
		} else if (c.getCount() > 1) {
			Log.d(WMSSQLiteDatabase.class.getName(),
					"More than one user has the same username");
			throw new SQLException();
		}

		return out;
	}

	@SuppressWarnings("deprecation")
	@Override
	public void updateAccount(Account account) {
		if (accountExists(account.getEmail())) {
			ContentValues cvUpdate = new ContentValues();
			cvUpdate.put(MyDbHelper.KEY_NAME, account.getEmail());
			cvUpdate.put(MyDbHelper.KEY_USER_PASSWORD, account.getPassword());

			Date accountDate = account.getAccountDate();
			String date = accountDate.getMonth() + "-" + accountDate.getDay()
					+ "-" + accountDate.getYear();
			cvUpdate.put(MyDbHelper.KEY_ACCOUNT_CREATED_DATE, date);
			cvUpdate.put(MyDbHelper.KEY_ACCOUNT_LOCKED,
					boolToInt(account.isLocked()));
			cvUpdate.put(MyDbHelper.KEY_IS_ADMIN, boolToInt(account.isAdmin()));
			cvUpdate.put(MyDbHelper.KEY_ACCOUNT_PHONE, account.getPhone());

			myDatabase.update(MyDbHelper.USER_TABLE_NAME, cvUpdate,
					MyDbHelper.KEY_NAME + "=?",
					new String[] { account.getEmail() });
		} else {
			addAccount(account);
		}

	}

	/**
	 * SQLite databases do not have a boolean data type, and instead uses
	 * integers of either 0(false) or 1(true). This method converts from a
	 * boolean to an int
	 * 
	 * @param in
	 *            Boolean to convert
	 * @return 1 or 0 depending on the input
	 */
	private int boolToInt(boolean in) {
		int out = 0;
		if (in) {
			out = 1;
		}
		return out;
	}

	/**
	 * SQLite databases do not have a boolean data type, and instead uses
	 * integers of either 0(false) or 1(true). This method converts from an int
	 * to a boolean
	 * 
	 * @param in
	 *            Int to convert
	 * @return true if in is 1 and false otherwise
	 */
	private boolean intToBool(int in) {
		boolean out = false;
		if (in == 1) {
			out = true;
		}
		return out;
	}

	/**
	 * Deletes all the items stored in the database
	 */
	public void deleteAllItems() {
		myDatabase.delete(MyDbHelper.ITEM_TABLE_NAME, null, null);
		myDatabase.delete(MyDbHelper.FOUND_ITEM_TABLE_NAME, null, null);
		myDatabase.delete(MyDbHelper.LOST_ITEM_TABLE_NAME, null, null);
	}

	@Override
	public List<Item> getDonatedItems(String username) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Item> getDonationRequestItems(String username) {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED };

		Cursor c = myDatabase.query(MyDbHelper.DONATION_REQUEST_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				MyDbHelper.KEY_ROWID + "=?",
				new String[] { MyDbHelper.DONATION_REQUEST_ITEM_ID }, null,
				null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToDonationRequestedItem(c));
		}

		return out;
	}

	@Override
	public List<Item> getAllDonatedItems() {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED };
		String whereClause = " 1=1 ";

		Cursor c = myDatabase.query(MyDbHelper.DONATED_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, null, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToDonatedItem(c));
		}

		return out;
	}

	/**
	 * Converts a cursor object to a donated item object
	 * 
	 * @param c
	 *            Cursor that was made from a query of the item tables
	 * @return The donated item object created from the cursor
	 */
	private Item cursorToDonatedItem(Cursor cursor) {
		String itemName = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_NAME));
		String description = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DESCRIPTION));
		String category = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_CATEGORY));
		boolean isResolved = intToBool(cursor.getInt(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_RESOLVE)));
		int lat = cursor.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LATITUDE));
		int lon = cursor
				.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LONGITUDE));
		byte[] pictureId = cursor.getBlob(cursor
				.getColumnIndex(MyDbHelper.KEY_PICTURE_ID));
		String username = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_USER_NAME));

		String dateEntered = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DATE_ENTERED));
		CustomDate dateE = new CustomDate(dateEntered);
		ItemLocation il = new ItemLocation(lat, lon);

		DonatedItem out = new DonatedItem(username, category, itemName,
				description, il, pictureId);
		out.setDateEntered(dateE);
		out.setResolved(isResolved);
		return out;
	}

	/**
	 * Converts a cursor object to a donation requested item object
	 * 
	 * @param c
	 *            Cursor that was made from a query of the item tables
	 * @return The donation requested item object created from the cursor
	 */
	private Item cursorToDonationRequestedItem(Cursor cursor) {
		String itemName = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_NAME));
		String description = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DESCRIPTION));
		String category = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_CATEGORY));
		boolean isResolved = intToBool(cursor.getInt(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_RESOLVE)));
		int lat = cursor.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LATITUDE));
		int lon = cursor
				.getInt(cursor.getColumnIndex(MyDbHelper.KEY_LONGITUDE));
		byte[] pictureId = cursor.getBlob(cursor
				.getColumnIndex(MyDbHelper.KEY_PICTURE_ID));
		String username = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_USER_NAME));

		String dateEntered = cursor.getString(cursor
				.getColumnIndex(MyDbHelper.KEY_ITEM_DATE_ENTERED));
		CustomDate dateE = new CustomDate(dateEntered);
		ItemLocation il = new ItemLocation(lat, lon);

		RequestedDonationItem out = new RequestedDonationItem(username,
				category, itemName, description, il, pictureId);
		out.setDateEntered(dateE);
		out.setResolved(isResolved);
		return out;
	}

	@Override
	public List<Item> getAllDonationRequestItems() {
		List<Item> out = new ArrayList<Item>();
		String[] columns = { MyDbHelper.KEY_ROWID, MyDbHelper.KEY_ITEM_NAME,
				MyDbHelper.KEY_ITEM_USER_NAME, MyDbHelper.KEY_CATEGORY,
				MyDbHelper.KEY_ITEM_DESCRIPTION, MyDbHelper.KEY_ITEM_RESOLVE,
				MyDbHelper.KEY_LONGITUDE, MyDbHelper.KEY_LATITUDE,
				MyDbHelper.KEY_PICTURE_ID, MyDbHelper.KEY_ITEM_DATE_ENTERED };
		String whereClause = " 1=1 ";

		Cursor c = myDatabase.query(MyDbHelper.DONATION_REQUEST_ITEM_TABLE_NAME
				+ " INNER JOIN " + MyDbHelper.ITEM_TABLE_NAME, columns,
				whereClause, null, null, null, null);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
			out.add(cursorToDonationRequestedItem(c));
		}

		return out;
	}
}
